package com.huangpuweb.task.controller;

import com.huangpuweb.task.model.SysHpRoadConditionInfo;
import com.huangpuweb.task.model.ZyDwdTfcOptSpeedRidAmapRt;
import com.huangpuweb.task.service.SyncSysDataService;
import com.huangpuweb.task.service.SyncZYJJZDataService;
import com.huangpuweb.task.util.DateUtil;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 同步道路信息定时任务
 */
@Controller
public class SyncRoadDataJob {

    private static Logger logger = Logger.getLogger(SyncRoadDataJob.class);


    private SyncSysDataService syncSysDataService;

    @Autowired
    public void setSyncSysDataService(SyncSysDataService syncSysDataService) {
        this.syncSysDataService = syncSysDataService;
    }

    private SyncZYJJZDataService syncZYJJZDataService;

    @Autowired
    public void setSyncZYJJZDataService(SyncZYJJZDataService syncZYJJZDataService) {
        this.syncZYJJZDataService = syncZYJJZDataService;
    }


    public void SyncRoadDataJobTask() throws Exception {

        LogFactory.getLog(this.getClass()).info("--------start-------SyncRoadDataJobTask----------" + new Date());

        /**
         * 同步各个景区路况信息到表hp_road_condition
         */
//        String[] spotCodeArr = Constants.SPOTCODEROAD.split(",");
        //SHHPQ_XINTIANDI,SHHPQ_YUYUAN_WAIQUAN,SHHPQ_WAITAN,SHHPQ_RENMINGUANGCHANG,SHHPQ_TIANZIFANG,SHHPQ_NANJINGLU
//        for (String spotname : spotCodeArr) {
//
//            String[] roadNames = new String[]{};
//            if (spotname.equals("SHHPQ_XINTIANDI")) {
//
//                roadNames = Constants.SHHPQXINTIANDIROAD.split(",");
//            } else if (spotname.equals("SHHPQ_YUYUAN_WAIQUAN")) {
//
//                roadNames = Constants.SHHPQYUYUANWAIQUANROAD.split(",");
//            } else if (spotname.equals("SHHPQ_WAITAN")) {
//
//                roadNames = Constants.SHHPQWAITANROAD.split(",");
//            } else if (spotname.equals("SHHPQ_RENMINGUANGCHANG")) {
//                roadNames = Constants.SHHPQRENMINGUANGCHANGROAD.split(",");
//            } else if (spotname.equals("SHHPQ_TIANZIFANG")) {
//                roadNames = Constants.SHHPQTIANZIFANGROAD.split(",");
//            } else if (spotname.equals("SHHPQ_NANJINGLU")) {
//                roadNames = Constants.SHHPQNANJINGLUROAD.split(",");
//            }
//
//            List<String> listRoadIds=syncSysDataService.getRoadIdsFromHpRoadConditionByRoadName(roadNames);
//
//
//            roadIds.addAll(listRoadIds);
//        }
        List<String> roadIds = new ArrayList<String>();
        roadIds = syncSysDataService.getRoadIdsFromHpRoadCondition();

        String updateTime = DateUtil.getDateTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ZyDwdTfcOptSpeedRidAmapRt> listzy = null;
        for (String roadId : roadIds) {

            listzy = syncZYJJZDataService.getZyDwdTfcOptSpeedRidAmapRtByRid(roadId);


            for (ZyDwdTfcOptSpeedRidAmapRt record : listzy) {
                if (null != record) {
                    SysHpRoadConditionInfo roadPredictionInfo = new SysHpRoadConditionInfo();
                    //设置路段名称
                    roadPredictionInfo.setRoadId(roadId);
                    //设置路段不等待红绿速度
                    if (record.getNostopSpeed() != null) {
                        Double nostop_speed = Double.valueOf(record.getNostopSpeed());
                        roadPredictionInfo.setNostopSpeed(nostop_speed);
                    } else {
                        roadPredictionInfo.setNostopSpeed(0);
                    }
                    //设置路段等待红绿灯速度
                    if (record.getSpeed() != null) {
                        Double speed = Double.valueOf(record.getSpeed());
                        roadPredictionInfo.setSpeed(speed);
                    } else {
                        roadPredictionInfo.setSpeed(0);

                    }
                    //设置路段等级
                    if (record.getRoadLevel() != null) {
                        Integer road_level = Integer.valueOf(record.getRoadLevel());
                        roadPredictionInfo.setRoadLevel(road_level);
                    } else {
                        roadPredictionInfo.setRoadLevel(0);
                    }
                    //新加三个字段
                    if (record.getLen() != null) {
                        roadPredictionInfo.setLen(record.getLen());
                    } else {
                        roadPredictionInfo.setLen(0d);
                    }
                    if (record.getTravel_time() != null) {
                        roadPredictionInfo.setTravel_time(record.getTravel_time());
                    } else {
                        roadPredictionInfo.setTravel_time(0d);
                    }
                    if (record.getNostop_travel_time() != null) {
                        roadPredictionInfo.setNostop_travel_time(record.getNostop_travel_time());
                    } else {
                        roadPredictionInfo.setNostop_travel_time(0d);
                    }
                    //设置数据记录时间

                    roadPredictionInfo.setRecordTime(sdf.format(record.getJhpt_update_time()));
                    //设置同步数据时间
                    roadPredictionInfo.setUpdateTime(updateTime);

                    syncSysDataService.insertOrUpdateRoadPredictionInfo(roadPredictionInfo);
                }
            }


        }


        LogFactory.getLog(this.getClass()).info("--------end-------SyncRoadDataJobTask----------" + new Date());

    }


}
